package com.mxt.data.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mxt.common.vo.Result;
import com.mxt.data.entity.Data;
import com.mxt.data.mapper.DataMapper;
import com.mxt.devices.entity.Devices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mxt
 * @since 2023-06-14
 */

@RestController
//@CrossOrigin 注解实现跨域请求，允许客户端跨域访问该 API
// 将请求路径映射为 /example/data
@CrossOrigin
@RequestMapping("example/data")
public class DataController {
    //使用 @Autowired 注解自动注入一个 DataMapper 对象，用于访问数据库。
        @Autowired
        private DataMapper dataMapper;

    @GetMapping
    public Result listData(){
        List<Data> list = dataMapper.selectList(null);
        return Result.success(list);
    }

    @GetMapping("/pageNow")
    public Result getPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startOfDay = now.with(LocalTime.MIN);
        LocalDateTime endOfDay = now.with(LocalTime.MAX);
        // 构建查询条件
        QueryWrapper<Data> queryWrapper= new QueryWrapper<>();
        queryWrapper.ge("update_time", startOfDay);
        queryWrapper.lt("update_time", endOfDay);
        Page<Data> page = new Page<>(pageNum,pageSize);
        Page<Data> dataPage = dataMapper.selectPage(page, queryWrapper);
        List<Data> list = dataPage.getRecords();
        return Result.success(list);
    }

    @GetMapping("/pageAll")
    public Result getAllPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        // 创建一个 Page 对象，指定要查询的页码和每页显示的记录数
        Page<Data> page = new Page<>(pageNum,pageSize);
        Page<Data> dataPage = dataMapper.selectPage(page,null);
        //数据
        List<Data> list = dataPage.getRecords();

        return Result.success(list);
    }

    @GetMapping("/search")
    public Result selectDevice(Integer input,String startDate,String endDate){
        //MyBatis-Plus 框架提供的查询条件构造器
        QueryWrapper<Data> wrapper = new QueryWrapper<>();
        if (input !=null){
            wrapper.eq("device_id",input);
        }
        if (startDate !=null && endDate!=null){
            wrapper.ge("update_time",startDate);
            wrapper.le("update_time",endDate);
        }
        List<Data> list = dataMapper.selectList(wrapper);
        return Result.success(list);
    }

    @GetMapping("/searchNow")
    public Result searchNowData(@RequestParam Integer deviceId){

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startOfDay = now.with(LocalTime.MIN);
        LocalDateTime endOfDay = now.with(LocalTime.MAX);
        // 构建查询条件
        QueryWrapper<Data> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("update_time", startOfDay);
        queryWrapper.lt("update_time", endOfDay);
        queryWrapper.eq("device_id",deviceId);
        List<Data> list = dataMapper.selectList(queryWrapper);
        return Result.success(list);
    }

}
